package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.AlertDataHandleDto;
import com.zzyl.entity.AlertData;
import com.zzyl.entity.Device;
import com.zzyl.mapper.AlertDataMapper;
import com.zzyl.mapper.DeviceMapper;
import com.zzyl.service.AlertDataService;
import com.zzyl.vo.AlertDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description AlertDataServicelmpl
 * @Author zhengyuan&junjun
 * @Date 2024-11-04
 */
/**
 *
 * 分页查询报警数据
 *
 * ***/



@Service
public class AlertDataServicelmpl implements AlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;
    @Autowired
    private DeviceMapper deviceMapper;


    @Override
    public PageResponse<AlertDataVo> getDataPage(Integer pageNum, Integer pageSize, String deviceName, Long endTime, Integer id, Long startTime, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        LocalDateTime startData = null;
        LocalDateTime endData = null;
        if (startTime != null && endTime != null) {
            startData = LocalDateTimeUtil.of(startTime);
            endData = LocalDateTimeUtil.of(endTime);
        }


        Page<AlertDataVo> page = alertDataMapper.seletPage(deviceName, status, startData, endData);

        if (ObjectUtil.isNotEmpty(page)) {
            List<String> iotIdList = page.stream().map(AlertDataVo::getIotId).collect(Collectors.toList());
            List<Device> deviceList = deviceMapper.selectByIOTId(iotIdList);
            for (AlertDataVo alertDataVo : page.getResult()) {
                for (Device device : deviceList) {
                    if (alertDataVo.getIotId().equals(device.getIotId())){
                        alertDataVo.setAccessLocation(device.getRemark());
                    }
                }
            }
        }


        return PageResponse.of(page,AlertDataVo.class);



    }

    @Override
    public void handleAlertData(Long id, AlertDataHandleDto alertDataHandleDto) {

        AlertData alertData = alertDataMapper.selectByPrimaryKey(id);
        alertDataMapper.updateByPrimaryKeySelective(alertData);
        alertDataMapper.updateStatus(alertData);




    }


}








/***
    @Override
    public PageResponse<AlertDataVo> getpage(AlertDataPageQueryDto alertDataPageQueryDto) {
        PageHelper.startPage(pageNum, pageSize);
        LocalDateTime startData = null;
        LocalDateTime endData = null;
        if (startTime != null && endTime != null) {
            startData = LocalDateTimeUtil.of(startTime);
            endData = LocalDateTimeUtil.of(endTime);
        }


        Page<AlertDataVo> page = alertDataMapper.seletPage(deviceName, status, startData, endData);

        List<String> iotIdList = page.stream().map(AlertDataVo::getIotId).collect(Collectors.toList());
        List<Device> deviceList = deviceMapper.selectByIOTId(iotIdList);
        for (AlertDataVo alertDataVo : page.getResult()) {
            for (Device device : deviceList) {
                if (alertDataVo.getIotId().equals(device.getIotId())){
                    alertDataVo.setAccessLocation(device.getRemark());
                }
            }
        }
        return PageResponse.of(page,AlertDataVo.class);



    }
    /***
    @Override
    public Object adminPageQuery(AlertDataPageQueryDto alertDataPageQueryDto) {
        // 获取当前的用户
        String subject = UserThreadLocal.getSubject();
        UserVo userVo = JSONUtil.toBean(subject, UserVo.class);

        PageHelper.startPage(alertDataPageQueryDto.getPageNum(), alertDataPageQueryDto.getPageSize());
        LocalDateTime startTime = alertDataPageQueryDto.getStartTime() == null ? null : LocalDateTimeUtil.of(alertDataPageQueryDto.getStartTime());
        LocalDateTime endTime = alertDataPageQueryDto.getEndTime() == null ? null : LocalDateTimeUtil.of(alertDataPageQueryDto.getEndTime());

        Page<AlertDataVo> pageResult = alertDataMapper.adminPageQuery(alertDataPageQueryDto.getId(), alertDataPageQueryDto.getStatus(), alertDataPageQueryDto.getDeviceName(),userVo.getId(), startTime, endTime);
        return PageResponse.of(pageResult, AlertDataVo.class);


    }
    ***/



   /***
    @Override
    public PageResponse<AlertDataVo> getDataPage(Integer pageNum, Integer pageSize, String deviceName, Long endTime, Integer id, Long startTime, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        LocalDateTime startData = null;
        LocalDateTime endData = null;
        if (startTime != null && endTime != null) {
            startData = LocalDateTimeUtil.of(startTime);
            endData = LocalDateTimeUtil.of(endTime);
        }


        Page<AlertDataVo> page = alertDataMapper.seletPage(deviceName, status, startData, endData);

        List<String> iotIdList = page.stream().map(AlertDataVo::getIotId).collect(Collectors.toList());
        List<Device> deviceList = deviceMapper.selectByIOTId(iotIdList);
        for (AlertDataVo alertDataVo : page.getResult()) {
            for (Device device : deviceList) {
                if (alertDataVo.getIotId().equals(device.getIotId())){
                    alertDataVo.setAccessLocation(device.getRemark());
                }
            }
        }
        return PageResponse.of(page,AlertDataVo.class);
    }
    ***/

    /***
     @Override
    public PageResponse<AlertDataVo> getpage(AlertDataPageQueryDto alertDataPageQueryDto) {
        //获取当前用户
        String subject = UserThreadLocal.getSubject();
        UserVo userVo = JSONUtil.toBean(subject, UserVo.class);



        PageHelper.startPage(alertDataPageQueryDto.getPageNum(),alertDataPageQueryDto.getPageSize());
        LocalDateTime startTime = alertDataPageQueryDto.getStartTime()
                == null ? null : LocalDateTimeUtil.of(alertDataPageQueryDto.getStartTime());
        LocalDateTime endTime = alertDataPageQueryDto.getEndTime()
                == null ? null : LocalDateTimeUtil.of(alertDataPageQueryDto.getEndTime());

        Page<AlertDataVo> pageResult =
                alertDataMapper.seletPage(alertDataPageQueryDto.getId(),alertDataPageQueryDto.getStatus(),alertDataPageQueryDto.getDeviceName(),userVo.getId());
        //设置接入位置
         List<String> iotIdList = pageResult.stream().map(AlertDataVo::getIotId).collect(Collectors.toList());
         List<Device> deviceList =  deviceMapper.selectByIOTId(iotIdList);
         for (AlertDataVo alertDataVo : pageResult.getResult()) {
             for (Device device : deviceList) {
                 if (alertDataVo.getIotId().equals(device.getIotId())){
                     alertDataVo.setAccessLocation(device.getRemark());
                 }
             }
         }

         return PageResponse.of(pageResult,AlertDataVo.class);
    }
     **/



    /****
    @Override
    public PageResponse<AlertDataVo> getDataPage(Integer pageNum, Integer pageSize, String deviceName, Long endTime, Integer id, Long startTime, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        LocalDateTime startData = null;
        LocalDateTime endData = null;
        if (startTime != null && endTime != null) {
            startData = LocalDateTimeUtil.of(startTime);
            endData = LocalDateTimeUtil.of(endTime);
        }


        Page<AlertDataVo> page = alertDataMapper.seletPage(deviceName,status,startData,endData);

        return PageResponse.of(page,AlertDataVo.class);


    }
    ***/

 /***

    @Override
    public PageResponse<AlertDataVo> getDataPage(Integer pageNum, Integer pageSize, String deviceName, Long endTime, Integer id, Long startTime, Integer status) {


        PageHelper.startPage(pageNum,pageSize);
        LocalDateTime startData = null;
        LocalDateTime endData = null;
        if (startTime != null && endTime != null) {
            startData = LocalDateTimeUtil.of(startTime);
            endData = LocalDateTimeUtil.of(endTime);
        }


        Page<AlertDataVo> page = alertDataMapper.seletPage(deviceName,status,startData,endData);

        return PageResponse.of(page,AlertDataVo.class);
    }***/

